using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using System.Collections.Generic;

public partial class Admin_ErrorMessages : SFPage
{
    busErrorMessages insBusErrorMessages = new busErrorMessages();
    entErrorMessages insEntErrorMessages = new entErrorMessages();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgErrorMessages_NeedsDataSource()
    {
        DataTable insDataTableErrorMessages = insBusErrorMessages.SelectErrorMessages();
        if (insBusErrorMessages.HasErrors)
        {
            MessageBoxShow(insBusErrorMessages);
        }
        sfgErrorMessages.SetData(insDataTableErrorMessages);
        return insBusErrorMessages.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgErrorMessages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntErrorMessages.ErrorMessageId = Convert.ToInt32(e.CommandArgument);
            insBusErrorMessages.DeleteErrorMessagesByErrorMessageId(insEntErrorMessages);
            if (insBusErrorMessages.HasErrors)
            {
                MessageBoxShow(insBusErrorMessages);
                return;
            }
            sfgErrorMessages.LoadGridData();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstErrorMessageMultilanguageSettings.LoadDataListData();

    }
    protected bool lstErrorMessageMultilanguageSettings_NeedsDataSource()
    {
        insEntErrorMessages.ErrorMessageId = this.Id;
        insBusErrorMessages.SelectErrorMessagesByErrorMessageId(insEntErrorMessages);
        if (insBusErrorMessages.HasErrors)
        {
            MessageBoxShow(insBusErrorMessages);
            return true;
        }
        txtErrorMessageName.Text = insEntErrorMessages.ErrorMessageName;

        entErrorMessageMultilanguageSettings insEntErrorMessageMultilanguageSettings = new entErrorMessageMultilanguageSettings();
        insEntErrorMessageMultilanguageSettings.ErrorMessageId = insEntErrorMessages.ErrorMessageId;
        busErrorMessageMultilanguageSettings insBusErrorMessageMultilanguageSettings = new busErrorMessageMultilanguageSettings();
        DataTable insDataTableErrorMessageMultilanguageSettings = insBusErrorMessageMultilanguageSettings.SelectErrorMessageMultilanguageSettingsByErrorMessageId(insEntErrorMessageMultilanguageSettings);
        if (insBusErrorMessageMultilanguageSettings.HasErrors)
        {
            MessageBoxShow(insBusErrorMessageMultilanguageSettings);
            return true;
        }
        lstErrorMessageMultilanguageSettings.SetData(insDataTableErrorMessageMultilanguageSettings);
        return false;
    }


    List<entErrorMessageMultilanguageSettings> insListErrorMessageMultilanguageSettingsInsert = new List<entErrorMessageMultilanguageSettings>();
    List<entErrorMessageMultilanguageSettings> insListErrorMessageMultilanguageSettingsUpdate = new List<entErrorMessageMultilanguageSettings>();
    List<entErrorMessageMultilanguageSettings> insListErrorMessageMultilanguageSettingsDelete = new List<entErrorMessageMultilanguageSettings>();
    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntErrorMessages.ErrorMessageName = txtErrorMessageName.Text;


        foreach (DataListItem dli in lstErrorMessageMultilanguageSettings.Items)
        {
            entErrorMessageMultilanguageSettings insEntErrorMessageMultilanguageSettings = new entErrorMessageMultilanguageSettings();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntErrorMessageMultilanguageSettings.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntErrorMessageMultilanguageSettings.ErrorMessageText = ((SFTextBox)dli.FindControl("txtErrorMessageText")).Text;

                if (lstErrorMessageMultilanguageSettings.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntErrorMessageMultilanguageSettings.ErrorMessageMultilanguageSettingId = Convert.ToInt32(lstErrorMessageMultilanguageSettings.DataKeys[dli.ItemIndex]);
                    insListErrorMessageMultilanguageSettingsUpdate.Add(insEntErrorMessageMultilanguageSettings);
                }
                else
                {
                    insListErrorMessageMultilanguageSettingsInsert.Add(insEntErrorMessageMultilanguageSettings);
                }
            }
            else
            {
                if (lstErrorMessageMultilanguageSettings.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntErrorMessageMultilanguageSettings.ErrorMessageMultilanguageSettingId = Convert.ToInt32(lstErrorMessageMultilanguageSettings.DataKeys[dli.ItemIndex]);
                    insListErrorMessageMultilanguageSettingsDelete.Add(insEntErrorMessageMultilanguageSettings);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusErrorMessages.InsertErrorMessages(insEntErrorMessages, insListErrorMessageMultilanguageSettingsInsert, insListErrorMessageMultilanguageSettingsUpdate, insListErrorMessageMultilanguageSettingsDelete);
        if (insBusErrorMessages.HasErrors)
        {
            MessageBoxShow(insBusErrorMessages);
            return;
        }
        base.SaveNew();
        sfgErrorMessages.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntErrorMessages.ErrorMessageId = this.Id;
        insBusErrorMessages.UpdateErrorMessagesByErrorMessageId(insEntErrorMessages, insListErrorMessageMultilanguageSettingsInsert, insListErrorMessageMultilanguageSettingsUpdate, insListErrorMessageMultilanguageSettingsDelete);
        if (insBusErrorMessages.HasErrors)
        {
            MessageBoxShow(insBusErrorMessages);
            return;
        }
        base.SaveExisting();
        sfgErrorMessages.LoadGridData();
    }

    protected void lstErrorMessageMultilanguageSettings_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["ErrorMessageMultilanguageSettingId"] != DBNull.Value);
                e.Item.FindControl("pnlErrorMessageMultilanguageSettings").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlErrorMessageMultilanguageSettings").Visible = ((SFCheckBox)sender).Checked;
    }
}
